<?php

namespace App\Http\Controllers\Admin\Article;

use Illuminate\Http\Request;
use App\Http\Requests;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Http\Model\Article;

class BookaddController extends Controller
{

    //文章添加
    public function Bookadd()
    {
        $article_name = request('article_name');
        $article_category = request('article_category');
        $desc = request('desc');
        if (empty($article_name && $article_category  && $desc)){
            return view('Admin/Article/bookadd');
        }
        $res = Article::create(['article_name'=>$article_name,'article_category'=>$article_category,'desc'=>$desc,'read_num'=>0]);

        if ($res) {
            return response()->json(['status'=>200, 'message'=>'success']);
        } else {
            return response()->json(['status'=>200, 'message'=>'fail']);
        }
    }

    //文章列表
    public function Booklist()
    {
        $list = Article::select()->paginate(10);
        $article_name = request('search');
        if (!empty($article_name)){
            $list = Article::where('article_name', 'like', "%{$article_name}%")->paginate(10);
        }
        return view('Admin/Article/booklist')->with('list',$list);
    }

     // 文章删除
    public function Bookdel($id)
    {
        return $res = Article::where(['id'=>$id])->delete();

    }

}
